package com.jandar.controller;

import com.cl.chlorine.dto.CommonResult;
import com.jandar.service.AnalyzeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/analyze")
public class AnalyzeController {
    @Autowired
    AnalyzeService analyzeService;

    @RequestMapping(path = "/listTable", method = RequestMethod.GET)
    public CommonResult<List<String>> listTable() {
        return CommonResult.success(analyzeService.listTable());
    }
    @RequestMapping(path = "/listField", method = RequestMethod.GET)
    public CommonResult<List<Map<String,String>>> listField(String tableName) {
        return CommonResult.success(analyzeService.listField(tableName));
    }
    @RequestMapping(path = "/download", method = RequestMethod.GET)
    public ResponseEntity<InputStreamResource> downloadIncrementFilterResult(@RequestParam(name = "id") Integer id, @RequestParam(name = "type") String type) throws FileNotFoundException {
        File excelFile = analyzeService.getFile(id, type);
        if (excelFile != null && excelFile.exists()) {
            return ResponseEntity.ok()
                    .header("content-disposition", "attachment; filename=" + excelFile.getName())
                    .contentLength(excelFile.length())
                    .contentType(MediaType.parseMediaType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"))
                    .body(new InputStreamResource(new FileInputStream(excelFile)));
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    @RequestMapping("upload/{typeId}/{personId}/{caseNum}")
    public ResponseEntity uploadFile(MultipartFile[] file, @PathVariable("typeId") Integer typeId, @PathVariable("personId") Integer personId, @PathVariable("caseNum") String caseNum, HttpServletRequest request) {
        for (MultipartFile multipartFile : file) {
            analyzeService.uploadFile(multipartFile, typeId, personId, caseNum, request);

        }
        return ResponseEntity.ok("上传成功");
    }

    @RequestMapping("upload/{personId}/{caseNum}")
    public ResponseEntity uploadFile(MultipartFile[] file, @PathVariable("personId") Integer personId, @PathVariable("caseNum") String caseNum, HttpServletRequest request) {
        for (MultipartFile multipartFile : file) {
            analyzeService.uploadFile(multipartFile, null, personId, caseNum, request);

        }
        return ResponseEntity.ok("上传成功");
    }
    @RequestMapping("upload/{typeId}/{caseNum}")
    public ResponseEntity uploadFile(MultipartFile file, @PathVariable("typeId") Integer typeId, @PathVariable("caseNum") String caseNum, HttpServletRequest request) {
        analyzeService.uploadFile(file, typeId, null, caseNum, request);
        return ResponseEntity.ok("上传成功");
    }
    @RequestMapping("upload/{caseNum}")
    public ResponseEntity uploadFile(MultipartFile file,  @PathVariable("caseNum") String caseNum, HttpServletRequest request) {
        analyzeService.uploadFile(file, null, null, caseNum, request);
        return ResponseEntity.ok("上传成功");
    }

    @GetMapping("listExcelSourceType")
    public ResponseEntity listExcelSourceType(String caseNum,Integer personId){
        return ResponseEntity.ok(analyzeService.listExcelSourceType(caseNum, personId));
    }
    @PostMapping("pageDataByType")
    public ResponseEntity pageDataByType(@RequestBody AnalyzeService.PageParam pageParam){
        return ResponseEntity.ok(analyzeService.pageDataByType(pageParam));
    }
    @PostMapping("previewSourceData")
    public ResponseEntity previewSourceData(@RequestBody AnalyzeService.PageParam pageParam){
        return ResponseEntity.ok(analyzeService.previewSourceData(pageParam));
    }

    @PostMapping("pagePerson")
    public ResponseEntity pagePerson(@RequestBody AnalyzeService.PageParam pageParam){
        return ResponseEntity.ok(analyzeService.pagePerson(pageParam));
    }
    @PostMapping("frequentPerson")
    public ResponseEntity frequentPerson(@RequestBody AnalyzeService.PageParam pageParam) {
        return ResponseEntity.ok(analyzeService.frequentPerson(pageParam));
    }

    @PostMapping("caseTimeline")
    public ResponseEntity caseTimeline(@RequestBody AnalyzeService.PageParam param) {
        return ResponseEntity.ok(analyzeService.caseTimeline(param));
    }
    @PostMapping("sendAndIncomeStatistics")
    public ResponseEntity sendAndIncomeStatistics(@RequestBody AnalyzeService.PageParam param) {
        return ResponseEntity.ok(analyzeService.sendAndIncomeStatistics(param));
    }



    @PostMapping("page")
    public ResponseEntity page(@RequestBody AnalyzeService.PageParam pageParam, HttpServletRequest request) {
        return ResponseEntity.ok(analyzeService.page(pageParam, request));
    }

    @PostMapping("pageData")
    public ResponseEntity pageData(@RequestBody AnalyzeService.PageParam pageParam) {
        return ResponseEntity.ok(analyzeService.pageData(pageParam));
    }

//    @PostMapping("pageDataByPersonId")
//    public ResponseEntity pageDataByPersonId(@RequestBody AnalyzeService.PageParam pageParam) {
//        return ResponseEntity.ok(analyzeService.pageDataByPersonId(pageParam));
//    }

    @GetMapping("delete")
    public ResponseEntity delete(Integer id) {
        return ResponseEntity.ok(analyzeService.delete(id));
    }


//    @PostMapping("processBankDataApi")
//    public ResponseEntity processBankDataApi(@RequestBody ProcessBankDataApiParam param) {
//        analyzeAndStatisticService.processBankDataApi(param.data, param.mappingList);
//        return ResponseEntity.ok("success");
//    }
//
//    @Data
//    static class ProcessBankDataApiParam{
//        List<Map> data;
//        List<Map<String, Object>> mappingList;
//    }


}
